/**
 * <p>文本框, 可以获取用户的输入文本</p>
 * 
 * @class 文本框控件类
 */
zxt.ui.Textbox = function(required, tip, vd) {

	zxt.ui.Element.call(this);
	
	this.$TextAlign = null;
	this.$TextTransform = zxt.cfg.TextTransform;
	this.$MaxLength = null;
	this.$COLOR = null;
	

	this._$input = null;
	this._$jqDom = null;

	this._tip = tip;// 提示信息
	this._isTip = false;
	this._validator = vd;
	this._required = (required == true || required == "true");
	this._formatType = null;
};

/**
 * <p>根据传入的DOM元素, 解析传入的相关属性, 创建相应的文本框对象</p>
 * 
 * @function
 * @static
 * @param {DOM} elem 解析的DOM元素
 * @return {zxt.ui.Label} 根据传入的DOM元素创建的文本框对象
 */
zxt.ui.Textbox.parse = function(elem) {
	var j = $(elem);

	var _val = j.attr("zxt.value");
	var _required = j.attr("zxt.required");
	var _regex = j.attr("zxt.regex");
	var _regexPromot = j.attr("zxt.regexPrompt");
	var _type = j.attr("zxt.type");
	var _tip = j.attr("zxt.tip");
	var _formatType = j.attr("zxt.formatType");

	var _tv = zxt.core.ValidatorManager.getValidator(_type);
	var _rv = null;

	if (zxt.util.isAvailable(_regex)) {
		_rv = new zxt.core.ValidatorElement();
		var reg = new RegExp(_regex);
		_rv.regex(reg);
		_rv.prompt(_regexPromot || _regex);
	};

	var vds = null;
	if (zxt.util.isAvailable(_tv) && zxt.util.isAvailable(_rv)) {
		vds = new zxt.core.ValidatorChain();
		vds.chain(_tv).chain(_rv);
	} else if (zxt.util.isAvailable(_tv)) {
		vds = _tv;
	} else {
		vds = _rv;
	};

	var _instance = new zxt.ui.Textbox(_required, _tip, vds);
	_instance.val(_val);
	_instance.formatType(_formatType);
	
	var _hide = j.attr("zxt.hide") || "";	
	_instance.$Hide = _hide; 
	
	return _instance;
};

zxt.defineClazz(zxt.ui.Textbox, zxt.ui.Element);

/**
 * <p>返回控件类型</p>
 * 
 * @function
 * @returns {string} "zxt.ui.Textbox"
 */
zxt.ui.Textbox.prototype.getType = function() {
	return "zxt.ui.Textbox";
};

/**
 * <p>获取标签值</p>
 * @function
 * @return
 */
zxt.ui.Textbox.prototype.getSelfLabel = function(){
	return this.val();
};

/**
 * <p>如果参数没上送，则返回当前的显示格式设置；否则，设置显示格式</p>
 * @param  {String} v 显示格式
 * @return {String} 如果参数没上送，则返回当前的显示格式设置
 */
zxt.ui.Textbox.prototype.formatType = function(f){
	if (zxt.util.isAvailable(f)) {this._formatType=f};
	return this._formatType;
}


/**
 * <p>验证值的合法性</p>
 * 
 * @function
 * @returns {boolean} 验证通过，则返回true,否则，返回false
 */
zxt.ui.Textbox.prototype.validate = function(value) {
	var rs = [];
	if (!zxt.util.notValue(this._validator)) {
		rs = this._validator.validate(value);
		this.validateStatus(false,zxt.core.ValidatorType.REGEX,this._validator);
	};
	if (!zxt.util.utilsFormat(this.val(),this.formatType())) {
		rs.push("utilsFormat error");
		this.validateStatus(false,zxt.core.ValidatorType.FORMAT,this.formatType());
	}
	return rs;
};

zxt.ui.Textbox.prototype._decorate = function(show, toValidate) {
	if ( toValidate === undefined ){ toValidate = true; }
	this._isTip = false;
	
	this._$input.removeClass("zxt-txhint");
	
	var iVal = this.val();
	var iLabel = this.getLabel();
	
	if ( (!zxt.util.isAvailable(iVal)) || !zxt.util.isAvailable(iLabel) ) {
		this._$input.removeClass("zxt-txerror").removeClass("zxt-txpass");
		if (this._required){
			this._$input.removeClass("zxt-txnormal").addClass("zxt-txmust");
			this.validateStatus(false,zxt.core.ValidatorType.REQUIRED);
		} else { 
			this._$input.removeClass("zxt-txmust").addClass("zxt-txnormal");
			this.validateStatus(true);
		};		
				
		if ( zxt.util.isValue(this._tip) && show ){
			this._$input.val(this._tip);
			this._isTip = true;
			this._$input.addClass("zxt-txhint");
		} else {
			this._$input.val("");
		};	
		return;
	};	

	this._$input.removeClass("zxt-txmust").removeClass("zxt-txnormal");
	
	var ov = this._$input.val();	
	if ( ov != iLabel ){
		this._$input.val(iLabel);
		zxt.dom.setCursorAtEnd(this._$input.get(0));
	};
	
	if ( toValidate )	{
		var errs = this.validate(this.val());	
		if (errs.length == 0) {
			this._$input.addClass("zxt-txpass").removeClass("zxt-txerror");
			this.validateStatus(true);
		} else {
			this._$input.addClass("zxt-txerror").removeClass("zxt-txpass");
		};		
	};
};

zxt.ui.Textbox.prototype.beforeGetValue = function() {
	
	if ( this.mode() != "NORMAL" ){
		return;
	};

	if ( zxt.util.isValue(this._$input) && (!this._isTip) ){
		var inputV = this._$input.val();
		this.val(inputV.trim());
	};
};

/**
 * <p>返回文本框中输入的字符串</p>
 * 
 * @function
 * @returns {string} 文本框中的值
 */
zxt.ui.Textbox.prototype.getSelfValue = function() {	
	if ( zxt.util.notValue(this.val()) ){
		return "";
	};
	
	var val = this.val();
	if(this.$TextTransform=="uppercase"){
			val = val.toUpperCase();
	};
	
	for( var i=0; i<this._hides().length;i++ ){
		if ( this._hides()[i] == val ){ val = ""; }
	};
		
	return val;	
};

/**
 * <p>
 * 渲染该文本框的DOM节点
 * </p>
 * 
 * @function
 * @return {DOM} 渲染成的DOM节点
 */
zxt.ui.Textbox.prototype.renderSelf = function() {
	var instance = this;
	
	if (this._$jqDom==null){
		this._$jqDom = $("<div nowrap class='zxt-Element'>");
	};	
	
	if ( this._$input == null ){
		this._$input = $("<input type='text' class='zxt-textbox'/>");		
		
		if (zxt.util.isAvailable(this.id()) && zxt.cfg.nameNative) {
			this._$input.attr("name", this.id());
		};	
		
		this._$input.width("100%");			
		this._$jqDom.empty().append(this._$input);
	};	
	this._$input.blur(function() {instance.onBlur();});//绑定失去焦点事件
	this._$input.focus(function(){ instance.onFocus();});	//绑定获得焦点事件
	
	if ( zxt.util.isValue(this.$TextAlign) ){
		this._$input.css("text-align", this.$TextAlign);
	};
	if ( zxt.util.isValue(this.$TextTransform) ){
		this._$input.css("text-transform", this.$TextTransform);
	};	
	if ( zxt.util.isValue(this.$MaxLength) ){
		this._$input.attr("maxLength", this.$MaxLength);
	};
	if ( zxt.util.isValue(this.$COLOR) ){
		this._$input.css("color", this.$COLOR);
	};
	this._decorate(true, true);

	if(this.mode() == "DISABLE"){
		this._$input.removeClass();
		this._$input.addClass("zxt-textbox");
		this._$input.attr("disabled",true);
	}else if(this.mode() == "READONLY"){
		this._$input.removeClass();
		this._$input.addClass("zxt-textbox");
		this._$input.attr("readonly",true);
	}else{
		this._$input.attr("disabled",false);
	};	
	
	return this._$jqDom.get(0);
};

/**
 * <p>设置当前控件焦点</p>
 * 
 * @function
 * @returns 
 */
zxt.ui.Textbox.prototype.focusSelf = function(){		
	this._$input.focus();	
	this._$input.select();
};

/**
 * <p>失去当前控件焦点</p>
 * 
 * @function
 * 
 */
zxt.ui.Textbox.prototype.blurSelf = function(){
	this._$input.blur();
};

/**
 * <p>
 * 控件获得焦点事件
 * </p>
 * 
 * @private
 * @event
 */
zxt.ui.Textbox.prototype.whenFocus = function() {
	this._decorate(false, false);		
};

/**
 * <p>
 * 控件失去焦点事件
 * </p>
 * 
 * @private
 * @event
 */
zxt.ui.Textbox.prototype.whenBlur = function() {
	if ( !this._isTip ){
		var inputV = this._$input.val();
		this.val(inputV.trim());
	};
	this._decorate(true, true);	
};




/**
 * <p>文本框工厂类</p>
 * @class 
 */
zxt.ui.TextboxFactory = function() {
};

zxt.defineClazz(zxt.ui.TextboxFactory, zxt.ui.InteractorFactory);

/**
 * <p>获取相应的输入框实例对象</p>
 * 
 * @function
 * @return {zxt.ui.Textbox} 输入框实例对象
 */
zxt.ui.TextboxFactory.prototype.getInstance = function() {
	return new zxt.ui.Textbox();
};



/**
 * <p>多行文本框, 可以获取用户的输入大批量文本</p>
 * @param {String/Boolean} required 是否为必填项，默认为false
 * @param {String} tip 提示信息
 * @class 多行文本框控件类
 */
zxt.ui.TextArea = function(required, tip) {
	zxt.ui.Textbox.call(this);

	this._$textarea = null;
	this._$jqDom = $("<div nowrap class='zxt-Element'>");

	this._width="100%";
	this._height="100%";

	this._tip = tip;// 提示信息
	this._isTip = false;
	this._required = (required == true || required == "true");
};

/**
 * <p>根据传入的DOM元素, 解析传入的相关属性, 创建相应的文本框对象</p>
 * 
 * @function
 * @static
 * @param {DOM} elem 解析的DOM元素
 * @param {String/Boolean} zxt.required 是否为必填项，默认为false
 * @param {String} zxt.tip 提示信息
 * @param {String} zxt.value 初始值
 * @return {zxt.ui.Label} 根据传入的DOM元素创建的文本框对象
 */
zxt.ui.TextArea.parse = function(elem) {
	var j = $(elem);

	var _val = j.attr("zxt.value");
	var _required = j.attr("zxt.required");
	var _tip = j.attr("zxt.tip");

	var _width = j.attr("zxt.width");
	var _height = j.attr("zxt.height");

	var _instance = new zxt.ui.TextArea(_required, _tip);
	_instance.width(_width);
	_instance.height(_height);

	_instance.val(_val);
	return _instance;
};

zxt.defineClazz(zxt.ui.TextArea, zxt.ui.Textbox);

/**
 * <p>返回控件类型</p>
 * 
 * @function
 * @returns {string} "text"
 */
zxt.ui.TextArea.prototype.getType = function() {
	return "zxt.ui.TextArea";
};

/**
 * <p>控件宽度</p>
 * 
 * @function
 * @param {string} "v" 宽度 可以是%或者是px
 * @returns {string} 宽度
 */
zxt.ui.TextArea.prototype.width = function(v){
		if(v===undefined){return this._width;};
		this._width = v;
};

/**
 * <p>控件高度</p>
 * 
 * @function
 * @param {string} "v" 高度 可以是%或者是px
 * @returns {string} 高度
 */
zxt.ui.TextArea.prototype.height = function(v){
		if(v===undefined){return this._height;};
		this._height = v;
};

/**
 * <p>
 * 渲染该文本框的DOM节点
 * </p>
 * 
 * @function
 * @return {DOM} 渲染成的DOM节点
 */
zxt.ui.TextArea.prototype.renderSelf = function() {
	var instance = this;
		
	this._$input = $("<textarea class='zxt-textbox' style='overflow-y:auto'></textarea>");
	
	if (zxt.util.isAvailable(this.id()) && zxt.cfg.nameNative) {
		this._$input.attr("name", this.id());
	};

	this._$input.blur(function() { instance.onBlur();	});
	this._$input.focus(function() { instance.onFocus(); });

	this._$input.css("width",instance.width());
	this._$input.css("height",instance.height());
	
	this._decorate(true, true);

	if(this.mode() == "DISABLE"){
		this._$input.removeClass();
		this._$input.attr("disabled",true);
	}else if(this.mode() == "READONLY"){
		this._$input.removeClass();
		this._$input.attr("readonly",true);
	}else {
		this._$input.attr("disabled",false);
	};

	this._$jqDom.empty();
	this._$jqDom.append(this._$input);
	return this._$jqDom.get(0);
};

/**
 * <p>返回文本框中输入的字符串前的操作</p>
 * 
 * @function
 * @private
 */
zxt.ui.TextArea.prototype.beforeGetValue = function() {
	if ( zxt.util.isValue(this._$textarea) && (!this._isTip) ){
		this.val( this._$input.val() );
	};	
};

/**
 * <p>文本框工厂类</p>
 * @class 
 */
zxt.ui.TextAreaFactory = function() {
};

zxt.defineClazz(zxt.ui.TextAreaFactory, zxt.ui.InteractorFactory);

/**
 * <p>获取相应的输入框实例对象</p>
 * 
 * @function
 * @return {zxt.ui.TextArea} 输入框实例对象
 */
zxt.ui.TextAreaFactory.prototype.getInstance = function() {
	return new zxt.ui.TextArea();
};


/**
 * <p>密码框, 可以获取用户的输入密码</p>
 * 
 * @class 密码框控件类
 */
zxt.ui.PasswordBox = function(required, tip, vd, length) {
	zxt.ui.Textbox.call(this, required, tip, vd);

	this._length = length;
	this._$jqDom = $("<div nowrap class='zxt-Element'>");
};

zxt.defineClazz(zxt.ui.PasswordBox, zxt.ui.Textbox);

/**
 * <p>根据传入的DOM元素, 解析传入的相关属性, 创建相应的密码框对象</p>
 * 
 * @function
 * @static
 * @param {DOM} elem 解析的DOM元素
 * @return {zxt.ui.PasswordBox} 根据传入的DOM元素创建的文本框对象
 */
zxt.ui.PasswordBox.parse = function(elem) {
	var j = $(elem);

	var _val = j.attr("zxt.value");
	var _required = j.attr("zxt.required");
	var _regex = j.attr("zxt.regex");
	var _regexPromot = j.attr("zxt.regexPrompt");
	var _type = j.attr("zxt.type");
	var _tip = j.attr("zxt.tip");
	var _length = j.attr("zxt.length");	

	var _tv = zxt.core.ValidatorManager.getValidator(_type);
	var _rv = null;

	if (zxt.util.isAvailable(_regex)) {
		_rv = new zxt.core.ValidatorElement();
		var reg = new RegExp(_regex);
		_rv.regex(reg);
		_rv.prompt(_regexPromot || _regex);
	};

	var vds = null;
	if (zxt.util.isAvailable(_tv) && zxt.util.isAvailable(_rv)) {
		vds = new zxt.core.ValidatorChain();
		vds.chain(_tv).chain(_rv);
	} else if (zxt.util.isAvailable(_tv)) {
		vds = _tv;
	} else {
		vds = _rv;
	};

	var _instance = new zxt.ui.PasswordBox(_required, _tip, vds, _length);
	_instance.setValue(_val);
	return _instance;	
};

/**
 * <p>返回控件类型</p>
 * 
 * @function
 * @returns {string} "zxt.ui.PasswordBox"
 */
zxt.ui.PasswordBox.prototype.getType = function() {
	return "zxt.ui.PasswordBox";
};

/**
 * <p>重写了基类的render方法</p>
 * @function
 * @return
 */
zxt.ui.PasswordBox.prototype.render = function() {	
	var dom = null;
	
	if ( this._skeleton == null ){
		this._skeleton = $("<div class='zxt-skeleton' style='width:100%;'>");	
	}	
	
	if ( this.mode() == "LABEL" ){
		dom = $("<div class='zxt-Label'>");
		var label = this.getMask();
		dom.html(label);
	} else if ( this.mode() == "MASK") {
		dom = $("<div class='zxt-Label'>");
		dom.html(this.getMask());
	}	else {
		dom = this.renderSelf();		
	};	
	
	if ( dom != this._inner ){
		this._inner = dom;
		this._skeleton.empty();
		this._skeleton.append(dom);			
	};	
	return this._skeleton;
};

/**
 * <p>
 * 渲染该文本框的DOM节点
 * </p>
 * 
 * @function
 * @return {DOM} 渲染成的DOM节点
 */
zxt.ui.PasswordBox.prototype.renderSelf = function() {
	var instance = this;
	this._$input = $("<input type='password' class='zxt-textbox'/>");	
	
	if (zxt.util.isAvailable(this.id()) && zxt.cfg.nameNative) {
		this._$input.attr("name", this.id());
	};
	
	if ( zxt.util.isValue(this.$MaxLength) ){
		this._$input.attr("maxLength", this.$MaxLength);
	};
	
	this._$input.width("100%");
	this._$input.blur(function() {
		instance.onBlur();
	});
	this._$input.focus(function() {
		instance.onFocus();
	});
	
	this._decorate(true,true);	
	
	if(this.mode() == "DISABLE"){
		this._$input.removeClass();
		this._$input.attr("disabled",true);
	}else if(this.mode() == "READONLY"){
		this._$input.removeClass();
		this._$input.attr("readonly",true);
	}else {
		this._$input.attr("disabled",false);
	};

	this._$jqDom.empty();
	this._$jqDom.append(this._$input);
	return this._$jqDom.get(0);
};

/**
 * <p>[MASK]模式下获取加密控件的值</p>
 * @function
 * @return
 */
zxt.ui.PasswordBox.prototype.getMask = function(){
	return "xxxxxx";
};

/**
 * <p>获得焦点事件</p>
 * @function
 * @return
 */
zxt.ui.PasswordBox.prototype.whenFocus = function() {
	this._decorate(false,false);
	this._$input.get(0).select();
};

/**
 * <p>文本框工厂类</p>
 * @class 
 */
zxt.ui.PasswordBoxFactory = function() {
};

zxt.defineClazz(zxt.ui.PasswordBoxFactory, zxt.ui.InteractorFactory);

/**
 * <p>获取相应的输入框实例对象</p>
 * 
 * @function
 * @return {zxt.ui.PasswordBox} 输入框实例对象
 */
zxt.ui.PasswordBoxFactory.prototype.getInstance = function() {
	return new zxt.ui.PasswordBox();
};

/**
 * <p>卡号输入框, 可以获取用户的输入的银行卡号</p>
 * 
 * @class 文本框控件类
 * @param {string} [required] 是否为必填项 默认为false
 * @param {string} [tip] 显示的提示内容
 *
 */
zxt.ui.Cardidbox = function(required, tip) {
	zxt.ui.Textbox.call(this);
	this._$input = null;
	this._$jqDom = null;
	this._tip = tip;// 提示信息
	this._required = (required == true || required == "true");
};

/**
 * <p>根据传入的DOM元素, 解析传入的相关属性, 创建相应的卡号输入框对象</p>
 * 
 * @function
 * @static
 * @param {DOM} elem 解析的DOM元素
 * @param {string} [zxt.value] 初始值
 * @param {string} [zxt.required] 是否为必填项 默认为false
 * @param {string} [zxt.tip] 显示的提示内容
 * @return {zxt.ui.Cardidbox} 根据传入的DOM元素创建的卡号输入框对象
 */
zxt.ui.Cardidbox.parse = function(elem) {
	var j = $(elem);
	var _val = j.attr("zxt.value");
	var _required = j.attr("zxt.required");
	var _tip = j.attr("zxt.tip");

	var _instance = new zxt.ui.Cardidbox(_required, _tip);

	_instance.val(_val);
	return _instance;
};

zxt.defineClazz(zxt.ui.Cardidbox, zxt.ui.Textbox);

/**
 * <p>返回控件类型</p>
 * 
 * @function
 * @returns {string} "zxt.ui.Cardidbox"
 */
zxt.ui.Cardidbox.prototype.getType = function() {
	return "zxt.ui.Cardidbox";
};



/**
 * <p>验证值的合法性</p>
 * 
 * @function
 * @returns {boolean} 返回true
 */
zxt.ui.Cardidbox.prototype.validate = function(value) {
	if (zxt.util.utilsFormat(value,"CARD")) {
		return [];
	};
	if (zxt.util.utilsFormat(value,"CARD-15")) {
		return [];
	};
	if (zxt.util.utilsFormat(value,"CARD-19")) {
		return [];
	};
	this.validateStatus(false,zxt.core.ValidatorType.CARD);
	return ["卡号格式错误！"];
};

/**
 * <p>
 * 渲染该卡号输入框的DOM节点
 * </p>
 * 
 * @function
 * @return {DOM} 渲染成的DOM节点
 */
zxt.ui.Cardidbox.prototype.renderSelf = function() {
	var instance = this;
	
	if ( this._$jqDom == null ){
		this._$jqDom = $("<div nowrap class='zxt-Element'>");
		this._$input = $("<input type=\"text\" class='zxt-textbox' maxLength = \"19\"/>");		
		this._$jqDom.empty();
		this._$jqDom.append(this._$input);		
	};
	
	this._$input.focus(function() {instance.onFocus();}).blur(function() {instance.onBlur();});
	this._$input.bind("paste",function(e){instance.onPaste();});
	
	if(zxt.diff.getFirefoxVersion()>3){
		this._$input[0].oninput = function(){
			var card = instance._$input.val();
			var cardnbr = instance._formconnect(card);
			instance._$input.val(cardnbr);
			instance._$input[0].maxLength=19;
		}
	}
	
	if (zxt.util.isAvailable(this.id()) && zxt.cfg.nameNative) {
		this._$input.attr("name", this.id());
	};

	this._$input.width("100%");	
	this._decorate(true,true);
	
	if(this.mode() == "DISABLE"){
		this._$input.removeClass();
		this._$input.attr("disabled",true);
	}else if(this.mode() == "READONLY"){
		this._$input.removeClass();
		this._$input.attr("readonly",true);
	}else {
		this._$input.attr("disabled",false);
	};
	
	return this._$jqDom.get(0);
};

/**
 * <p>黏贴事件</p>
 * @function
 * @return
 */
zxt.ui.Cardidbox.prototype.onPaste = function(){
	if(zxt.diff.getFirefoxVersion()>3){
		this._$input[0].maxLength=23;
	}
	var card = zxt.diff.getClipboard();
	if(card){
		var cardnbr = this._formconnect(card);
		zxt.diff.setClipboard(cardnbr);
	}
	return;
};

/**
 * <p>
 * 卡号控件设值函数
 * </p>
 * 
 * @function
 * 
 */
zxt.ui.Cardidbox.prototype.setValue = function(value){
	if( !zxt.util.isAvailable(value)){
		this.val("");
		return;
	};
	
	var errs= this.validate(value);	
	if( errs.length>0){
		this.val(value);
		return;
		//throw new Error(errs[0]);
	};	
	this.val(value);
};


/**
 * <p>
 * 卡号控件获得焦点事件
 * </p>
 * 
 * @function
 * 
 */
zxt.ui.Cardidbox.prototype.whenFocus = function() {
	this._decorate(false,false);
	var input = this._$input;
	if(zxt.util.isValue(this.val())){
		input.val(this.val());
	}else{
		input.val("");
	};
	this._$input.select();
	//zxt.dom.setCursorAtEnd(input.get(0));
};


/**
 * <p>
 * 卡号控件失去焦点事件
 * </p>
 * 
 * @function
 * 
 */
zxt.ui.Cardidbox.prototype.whenBlur = function() {
	if(!this._isTip){
		this.val(this._formconnect(this._$input.val()));
	}
	this._decorate(true,true);
};

zxt.ui.Cardidbox.prototype.beforeGetValue = function() {
};	

/**
 * <p>返回卡号输入框中输入的字符串</p>
 * 
 * @function
 * @returns {string} 卡号输入框中的值
 */
zxt.ui.Cardidbox.prototype.getSelfValue = function() {
	if ( zxt.util.isAvailable(this.val()) ){
		return this.val();
	}	
	return "";
};

/**
 * <p>返回格式处理后的卡号</p>
 * @function
 * @return
 */
zxt.ui.Cardidbox.prototype.getSelfLabel = function() {
	return this._formcut(this.val());
};

/**
 * <p>[MASK]模式下显示卡号值</p>
 * @return
 */
zxt.ui.Cardidbox.prototype.getMask = function() {
	return "xxxx xxxx xxxx xxxx";
};

/**
 * <p>切割卡号函数</p>
 * @private
 * @function
 * @param {string} str 需要切割的字符串
 * @return {string} cutstr切割完成的卡号
 */
zxt.ui.Cardidbox.prototype._formcut = function(str) {
	if (str == null) {
		return null;
	};
	strArray = new Array();
	var k = 0;
	for(var i = 0; i<str.length; i+=4){
		strArray[k] = str.substring(i, i+4);
		k++;
	}
	var cutstr = strArray.join(" ");
	return cutstr;
};

/**
 * <p>合并卡号函数</p>
 * @private
 * @function
 * @param {string} str 需要合并的字符串
 * @return {string} cstr 合并完成的卡号
 */
zxt.ui.Cardidbox.prototype._formconnect = function(str) {
	if (str == null) {
		return null;
	};
	str= str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
	strArray = new Array();
	strArray = str.split(" ");
	var cstr = strArray.join("");
	return cstr;
};

/**
 * <p>卡号输入框工厂类</p>
 * @class 
 */
zxt.ui.CardidboxFactory = function() {
};

zxt.ui.CardidboxFactory.prototype = new zxt.clazz();
zxt.ui.CardidboxFactory.prototype.inheritFrom(zxt.ui.InteractorFactory);

/**
 * <p>获取相应的卡号输入框实例对象</p>
 * 
 * @function
 * @return {zxt.ui.Cardidbox} 输入框实例对象
 */
zxt.ui.CardidboxFactory.prototype.getInstance = function() {
	return new zxt.ui.Cardidbox();
};

/**
 * <p>金额控件，支持输入小数</p>
 * @class
 * @param {int} scale 小数位数
 */
zxt.ui.Sum = function(scale){
	zxt.ui.Textbox.call(this);
	
	var p = scale.split(",");	
	this._maxLength = p[0];	
	this._scaleLength = p[1];	
	this.val("");
};

/**
 * <p>根据传入的DOM元素, 解析传入的相关属性, 创建相应的金额控件输入框对象</p>
 * @param {DOM} elem DOM节点对象
 * @return
 */
zxt.ui.Sum.parse = function(elem){
	var j = $(elem);
	var _scale = j.attr("zxt.Scale")||"5,2";

	var _instance = new zxt.ui.Sum(_scale);
	return _instance;
};

zxt.defineClazz(zxt.ui.Sum, zxt.ui.Textbox);

/**
 * <p>如果参数没有上送，则返回当前的最大长度；否则，重新设置最大长度</p>
 * @param {int} v 最大长度
 * @return {int} 如果参数没有上送，则返回当前的最大长度
 */
zxt.ui.Sum.prototype.maxLength = function(v){
	if(v===undefined){return this._maxLength;}
	this._maxLength = v;
};

/**
 * <p>如果参数没有上送，则返回当前的小数位数；否则，重新设置</p>
 * @param {int} v 小数位数
 * @return {int} 如果参数没有上送，则返回当前的小数位数
 */
zxt.ui.Sum.prototype.scaleLength = function(v){
	if(v===undefined){return this._scaleLength;}
	this._scaleLength = v;
};

/**
 * <p>校验方法</p>
 * @param {String} value 待校验值
 * @return {Array} 校验信息，如果校验通过，返回空数组
 */
zxt.ui.Sum.prototype.validate = function(value) {
	var account = this._cutSum(value);
	var num = account.join(""); 
	if(num.length>this.maxLength()){
		return ["输入值长度不合法"];
	};
	if(isNaN(num)){
		return ["输入值非法"];
	};
	if(account[1].length!=this.scaleLength()){
		return ["小数点后长度不合法"];
	};
	return [];
};


zxt.ui.Sum.prototype.whenBlur = function() {
	var value = this._$input.val();	
	var nv = zxt.util.stringNumberFormat(value, this._scaleLength );
	this.val(nv);
	this._decorate(true,true);	
};



zxt.ui.Sum.prototype._cutSum = function(val){
	var _account = val.split(".");
	return _account;
};

zxt.ui.Sum.prototype.setValue = function(v) {
	var nv = zxt.util.stringNumberFormat(v, this._scaleLength );
	this.val(nv);
};	

zxt.ui.Sum.prototype.getSelfLabel = function(){
	return this.val();
};

zxt.ui.Sum.prototype.getSelfValue = function() {	
	if ( zxt.util.notValue(this.val()) ){
		return "";
	};
	
	var val = this.val();
	return val;	
};


/**
 * <p>身份证件号输入框, 可以获取用户的输入的客户证件号</p>
 * 
 * @class 文本框控件类
 * @param {string} [required] 是否为必填项 默认为false
 * @param {string} [tip] 显示的提示内容
 *
 */
zxt.ui.UserIdentity= function(required, tip) {
	zxt.ui.Textbox.call(this);
	this._$input = null;
	this._$jqDom = $("<div nowrap class='zxt-Element'>");
	this._tip = tip;// 提示信息
	this._required = (required == true || required == "true");
};

/**
 * <p>根据传入的DOM元素, 解析传入的相关属性, 创建相应的卡号输入框对象</p>
 * 
 * @function
 * @static
 * @param {DOM} elem 解析的DOM元素
 * @param {string} [zxt.value] 初始值
 * @param {string} [zxt.required] 是否为必填项 默认为false
 * @param {string} [zxt.tip] 显示的提示内容
 * @return {zxt.ui.UserIdentity} 根据传入的DOM元素创建的卡号输入框对象
 */
zxt.ui.UserIdentity.parse = function(elem) {
	var j = $(elem);
	var _val = j.attr("zxt.value");
	var _required = j.attr("zxt.required");
	var _tip = j.attr("zxt.tip");

	var _instance = new zxt.ui.UserIdentity(_required, _tip);

	_instance.val(_val);
	return _instance;
};

zxt.defineClazz(zxt.ui.UserIdentity, zxt.ui.Textbox);

/**
 * <p>返回控件类型</p>
 * 
 * @function
 * @returns {string} "zxt.ui.UserIdentity"
 */
zxt.ui.UserIdentity.prototype.getType = function() {
	return "zxt.ui.UserIdentity";
};

/**
 * <p>验证值的合法性</p>
 * 
 * @function
 * @returns {Array} 返回校验信息，如果校验通过，则返回空数组
 */
zxt.ui.UserIdentity.prototype.validate = function(value) {	
	var ID_card = value;   
	var IDcardLen = ID_card.length;   
	var fMod = 11;            //身份证算法求模关键值   
	var oldIDLen = 15;   //旧身份证长度     
	var newIDLen = 18;       //新身份证长度   
	var yearFlag = "19";     //新身份证年份标志      
	var Wi = new Array();       //位权值数组   
	var varArray = new Array();   
	var birthday = '';  //生日   
	var checkDigit = '';    //校验位   
	var numSum = 0;         //数字和   
	var regStr = /^\d{15,17}([\dxX]{1})?$/;   
  
	if ((IDcardLen != 15) && (IDcardLen != 18)) {   
		return ["身份证长度必须为15或18位."];     
	}   
	if(!regStr.test(ID_card)){   
		return ["身份证号码必须为数字(18位的新身份证最后一位可以是x)."];      
	}   
	if(IDcardLen == 15){   
		birthday = "19" + ID_card.substring(6,8) + "-" + ID_card.substring(8,10) + "-" + ID_card.substring(10,12);   
		}else{   
		birthday = ID_card.substring(6,10) + "-" + ID_card.substring(10,12) + "-" + ID_card.substring(12,14);   
	}   
	if(! this._isDate(birthday)){   
		return ["身份证号码中的日期格式不对."];        
	}   
	if(IDcardLen == 18){   
		for(var i=0;i<newIDLen-1;i++){        
			var k = Math.pow(2,newIDLen-1-i);    
			Wi[i] = k % 11;    
		}    
		for(var i=0;i<IDcardLen-1;i++){   
			varArray[i] = ID_card.charAt(i);   
			varArray[i] = varArray[i]*Wi[i];   
			numSum = numSum + varArray[i];   
		}   
		checkDigit = 12 - numSum % 11;   
		switch (checkDigit) {   
			case 10:   
				checkDigit = 'X';   
				break;   
			case 11:   
				checkDigit = 0;   
				break;   
			case 12:   
				checkDigit = 1;   
				break;   
		}   
		// check last digit   
		if (ID_card.charAt(17).toUpperCase() != checkDigit) {   
			return ["身份证最后一位校验码不对."];        
		}   
	}   
	return [];
};

  
/**  
 * <p>日期格式验证(yyyy-mm-dd,其中月，日可以只有一位</p>
 * @function
 */
zxt.ui.UserIdentity.prototype._isDate = function(dateStr){   
	var B, D=0;   
	// Split into $1 $2 $3   
	B = /^(\d{4})-(\d|\d\d)-(\d|\d\d)$/.test(dateStr);   
	if (B) with (RegExp){   
		// $1 $2 $3 = Y M D   
		D = new Date($1, $2-1, $3);   
		// test Y M D valid   
		B = ((D.getMonth()==$2-1) && (D.getFullYear()==$1));   
	}   
	return B;   
}   

/**
 * <p>
 * 渲染该证件号输入框的DOM节点
 * </p>
 * 
 * @function
 * @return {DOM} 渲染成的DOM节点
 */
zxt.ui.UserIdentity.prototype.renderSelf = function() {
	var instance = this;
	
	if ( zxt.util.notValue(this._$input) ){
		this._$input = $("<input type=\"text\" class='zxt-textbox' />");
		this._$input.focus(function() {instance.onFocus();});
		this._$input.blur(function() {instance.onBlur();});
		this._$jqDom.empty();
		this._$jqDom.append(this._$input);
	};
 
	if (zxt.util.isAvailable(this.id()) && zxt.cfg.nameNative) {
		this._$input.attr("name", this.id());
	};
  this._$input.css("text-transform", "uppercase");
  
	this._$input.width("100%");	
	this._decorate(true,true);
	
	if(this.mode() == "DISABLE"){
		this._$input.removeClass();
		this._$input.attr("disabled",true);
	}else if(this.mode() == "READONLY"){
		this._$input.removeClass();
		this._$input.attr("readonly",true);
	}else {
		this._$input.attr("disabled",false);
	};
	
	return this._$jqDom.get(0);
};


/**
 * <p>
 * 证件号控件设置函数
 * </p>
 * 
 * @function
 * 
 */
zxt.ui.UserIdentity.prototype.setValue = function(value){
	if( !zxt.util.isAvailable(value)){
		this.val("");
		return;
	};
	
	var errs= this.validate(value);	
	if( errs.length>0){
		this.val(value);
		return;
		//throw new Error(errs[0]);
	};	
	this.val(value);
};


/**
 * <p>
 * 证件号控件获得焦点事件
 * </p>
 * 
 * @function
 * 
 */
zxt.ui.UserIdentity.prototype.whenFocus = function() {
	this._decorate(false,false);
	var input = this._$input;
	if(zxt.util.isValue(this.val())){
		input.val(this.val());
	}else{
		input.val("");
	};	
};


/**
 * <p>
 * 证件号控件失去焦点事件
 * </p>
 * 
 * @function
 * 
 */
zxt.ui.UserIdentity.prototype.whenBlur = function() {
	if(!this._isTip){
		this.val(this._$input.val());
	}
	this._decorate(true,true);
};

zxt.ui.UserIdentity.prototype.beforeGetValue = function() {
};	

/**
 * <p>返回证件号输入框中输入的字符串</p>
 * 
 * @function
 * @returns {string} 卡号输入框中的值
 */
zxt.ui.UserIdentity.prototype.getSelfValue = function() {
	if ( zxt.util.notValue(this.val()) ){
		return "";
	};
		
	var val = this.val();
	val = val.toUpperCase();		
	return val;	
};

/**
 * <p>获取身份证控件的输入值</p>
 * @function
 * @return
 */
zxt.ui.UserIdentity.prototype.getSelfLabel = function() {
	if ( zxt.util.notValue(this.val()) ){
		return "";
	};	
	return this.val().toUpperCase();
};

/**
 * <p>[MASK]模式的身份证控件的显示值</p>
 * @function
 * @return
 */
zxt.ui.UserIdentity.prototype.getMask = function() {
	return "[Identity: xxxx xxxx xxxx]";
};
